package org.ne.banksys.mapper;

import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.ne.banksys.entity.Card;
import org.ne.banksys.entity.User;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author wey
 * @date 2025/6/13 下午10:40 06
 **/


@Mapper

public interface CardMapper {

    @Select("select * from card where card_id=#{cardId}")
    Card selectById(String cardId);

    @Select("select * from card")
    List<Card> selectAll();

    @Update("update card set balance = #{amount} where card_id=#{cardId}")
    void updateById(@Param("cardId") String cardId,@Param("amount") BigDecimal amount);


    @Insert("insert into card(card_id,cur_id,saving_id,open_date,open_money,balance,pass,is_report_loss,customer_id)" +
            "values (#{cardId}, 'RMB', #{savingId}, #{openDate}, #{openMoney}, #{balance}, #{pass}, #{isReportLoss},#{customerId})")
    void add(Card card);

    @Update("update card set is_report_loss='是' where card_id=#{cardId}")
    void reportLoss(String cardId);

    @Update("update card set is_report_loss='否' where card_id=#{cardId}")
    void repostReturn(String cardId);

    @Delete("delete from card where card_id = #{cardId}")
    void deleteById(String cardId);


    @Update("update card set pass=#{password} where card_id=#{cardId}")
    void updatePass(@Param("cardId") String cardId, @Param("password") String password);

    @Update("UPDATE card set cur_id=#{curId}, saving_id=#{savingId} where card_id=#{cardId}")
    void update(Card card);

    @Select("select * from card where customer_id=#{customerId}")
    Card registerCard(@Param("customerId") Long customerId);


    @Select("select * from card where customer_id=#{customerId}")
    List<Card> selectByCustomerId(Long customerId);

    @Select("select * from card limit #{pageNum}, #{pageSize}")
    Page<Card> getPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);





    @Select("SELECT * FROM card c " +
            "WHERE (#{startDate} IS NULL OR c.open_date >= #{startDate}) " +
            "AND (#{endDate} IS NULL OR c.open_date <= #{endDate})")
    List<Card> findByOpenDateRange(
            @Param("startDate") Timestamp startDate,
            @Param("endDate") Timestamp endDate
    );

    @Select("SELECT * FROM card c " +
            "WHERE (#{minMoney} IS NULL OR ABS(c.balance) >= #{minMoney}) " +
            "AND (#{maxMoney} IS NULL OR ABS(c.balance) <= #{maxMoney})")
    List<Card> findByOpenMoneyRange(
            @Param("minMoney") BigDecimal minMoney,
            @Param("maxMoney") BigDecimal maxMoney
    );


    @Select("SELECT * FROM card c " +
            "WHERE (#{startDate} IS NULL OR c.open_date >= #{startDate}) " +
            "AND( #{endDate} IS NULL OR c.open_date <= #{endDate})" +
            "AND(#{minMoney} IS NULL OR ABS(c.balance) >= #{minMoney})" +
            "AND (#{maxMoney} IS NULL OR ABS(c.balance) <= #{maxMoney})")
    List<Card> findByConditions(
            @Param("startDate") Timestamp startDate,
            @Param("endDate") Timestamp endDate,
            @Param("minMoney") BigDecimal minMoney,
            @Param("maxMoney") BigDecimal maxMoney
    );
}
